import Vue from 'vue'
import VueRouter from 'vue-router'
import {
    Message
} from 'element-ui'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error);
};

Vue.use(VueRouter)

// const HelloWorld = () => import("@/components/HelloWorld.vue")
const Login = () =>
    import ('@/views/login/Login.vue')
const Layout = () =>
    import ('@/components/layout/Layout.vue')
const Home = () =>
    import ('@/views/home/Home.vue')
const Entry = () =>
    import ('@/views/entry/Entry.vue')
const PersonCenter = () =>
    import ('@/views/personCenter/PersonCenter.vue')
const User = () =>
    import ('@/views/user/User.vue')
const OperationLog = () =>
    import ('@/views/operationLog/OperationLog.vue')
const Internship = () =>
    import ('@/views/internship/Internship.vue')
const InternshipLog = () =>
    import ('@/views/internship/InternshipLog.vue')

const NoAuth = () =>
    import ('@/views/error/401.vue')
const NotFound = () =>
    import ('@/views/error/404.vue')

const EmployeeInfoCenter = () =>
    import ('@/views/employee-info/employeeInfoCenter.vue')
const TalentPool = () =>
    import ('@/views/employee-info/talentPool.vue')
const ReportInternship = () =>
    import ('@/views/report/internship.vue')
const ReportStationTrans = () =>
    import ('@/views/report/stationTrans.vue')
const ResportDeptTrans = () =>
    import ('@/views/report/deptTrans.vue')
const ReportEntryAndDism = () =>
    import ('@/views/report/entryAndDism')
const Departure = () =>
    import ('@/views/dismission/departure')
const DepartureRecord = () =>
    import ('@/views/dismission/record')


const routes = [{
        path: '',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '登录',
        },
    },
    {
        path: '/home',
        name: 'Layout',
        component: Layout,
        children: [{
                path: '/home',
                name: 'Home',
                component: Home,
                meta: {
                    title: '首页',
                    isTabsPage: true,
                },
            },

            {
                path: '/entry',
                name: 'Entry',
                component: Entry,
                meta: {
                    title: '员工入职',
                    isTabsPage: true,
                },
            },
            {
                path: '/dept',
                name: 'Dept',
                component: () =>
                    import ('../views/dept/Dept.vue'),
                meta: {
                    title: '部门管理',
                    isTabsPage: true,
                }
            },
            {
                path: '/dept-transfer',
                name: 'DeptTransfer',
                component: () =>
                    import ('../views/dept/DeptTransfer.vue'),
                meta: {
                    title: '员工部门调动',
                    isTabsPage: true,
                }
            },
            {
                path: '/dept-log',
                name: 'DeptLog',
                component: () =>
                    import ('../views/dept/DeptLog.vue'),
                meta: {
                    title: '调动记录',
                    isTabsPage: true,
                }
            },
            {
                path: '/station',
                name: 'Station',
                component: () =>
                    import ('../views/employee/Station.vue'),
                meta: {
                    title: '岗位管理',
                    isTabsPage: true,
                }
            },
            {
                path: '/station-transfer',
                name: 'StationTransfer',
                component: () =>
                    import ('../views/employee/StationTransfer.vue'),
                meta: {
                    title: '员工岗位调动',
                    isTabsPage: true,
                }
            },
            {
                path: '/station-log',
                name: 'StationLog',
                component: () =>
                    import ('../views/employee/StationLog.vue'),
                meta: {
                    title: '岗位管理',
                    isTabsPage: true,
                }
            },
            {
                path: '/user',
                name: 'User',
                component: User,
                meta: {
                    title: '用户管理',
                    isTabsPage: true,
                    requireAuth: true
                },
            },
            {
                path: '/operation-log',
                name: 'OperationLog',
                component: OperationLog,
                meta: {
                    title: '操作记录',
                    isTabsPage: true,
                    requireAuth: true
                },
            },
            {
                path: '/person-center',
                name: 'PersonCenter',
                component: PersonCenter,
                meta: {
                    title: '个人中心',
                    isTabsPage: true,
                },
            },
            {
                path: '/info',
                name: 'EmployeeInfoCenter',
                component: EmployeeInfoCenter,
                meta: {
                    title: '员工信息中心',
                    isTabsPage: true,
                }
            },
            {
                path: '/talent',
                name: 'TalentPool',
                component: TalentPool,
                meta: {
                    title: '人才库',
                    isTabsPage: true,
                }
            },
            {
                path: '/report-internship',
                name: 'ReportInternship',
                component: ReportInternship,
                meta: {
                    title: '实习转正报表',
                    isTabsPage: true,
                }
            }, {
                path: '/report-station',
                name: 'ReportStationTrans',
                component: ReportStationTrans,
                meta: {
                    title: '岗位调动报表',
                    isTabsPage: true,
                },
            },
            {
                path: '/report-dept',
                name: 'ResportDeptTrans',
                component: ResportDeptTrans,
                meta: {
                    title: '部门调动报表',
                    isTabsPage: true,
                },
            }, {
                path: '/report-month',
                name: 'ReportEntryAndDism',
                component: ReportEntryAndDism,
                meta: {
                    title: '人事月报',
                    isTabsPage: true,
                }
            }, {
                path: '/internship',
                name: 'Internship',
                component: Internship,
                meta: {
                    title: '试用期管理',
                    isTabsPage: true,
                }
            }, {
                path: '/internship-log',
                name: 'InternshipLog',
                component: InternshipLog,
                meta: {
                    title: '转正记录',
                    isTabsPage: true,
                }
            },
            {
                path: '/dismission',
                name: 'Departure',
                component: Departure,
                meta: {
                    title: '员工离职',
                    isTabsPage: true
                }
            }, {
                path: '/dismission-log',
                name: 'DepartureRecord',
                component: DepartureRecord,
                meta: {
                    title: '离职记录',
                    isTabsPage: true
                }
            }
        ],
    },
    {
        path: '/401',
        name: 'NoAuth',
        component: NoAuth,
        meta: {
            title: '无权限'
        }
    },
    {
        path: '*',
        name: 'NotFound',
        component: NotFound,
        meta: {
            title: '404'
        }
    },
]

// {
//   path: '/home',
//   name: 'Layout',
//   component: Layout,
//   children: [{
//           path: '/home',
//           name: 'Home',
//           component: Home,
//           meta: {
//               title: '首页',
//               isTabsPage: true,
//           },
//       },

//       {
//           path: '/entry',
//           name: 'Entry',
//           component: Entry,
//           meta: {
//               title: '员工入职',
//               isTabsPage: true,
//           },
//       },
//       {
//           path: '/dept',
//           name: 'Dept',
//           component: () =>
//               import ('../views/dept/Dept.vue'),
//           meta: {
//               title: '部门管理',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/dept-transfer',
//           name: 'DeptTransfer',
//           component: () =>
//               import ('../views/dept/DeptTransfer.vue'),
//           meta: {
//               title: '员工部门调动',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/dept-log',
//           name: 'DeptLog',
//           component: () =>
//               import ('../views/dept/DeptLog.vue'),
//           meta: {
//               title: '调动记录',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/station',
//           name: 'Station',
//           component: () =>
//               import ('../views/employee/Station.vue'),
//           meta: {
//               title: '岗位管理',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/station-transfer',
//           name: 'StationTransfer',
//           component: () =>
//               import ('../views/employee/StationTransfer.vue'),
//           meta: {
//               title: '员工岗位调动',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/station-log',
//           name: 'StationLog',
//           component: () =>
//               import ('../views/employee/StationLog.vue'),
//           meta: {
//               title: '岗位管理',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/user',
//           name: 'User',
//           component: User,
//           meta: {
//               title: '用户管理',
//               isTabsPage: true,
//               requireAuth: true
//           },
//       },
//       {
//           path: '/operation-log',
//           name: 'OperationLog',
//           component: OperationLog,
//           meta: {
//               title: '操作记录',
//               isTabsPage: true,
//               requireAuth: true
//           },
//       },
//       {
//           path: '/person-center',
//           name: 'PersonCenter',
//           component: PersonCenter,
//           meta: {
//               title: '个人中心',
//               isTabsPage: true,
//           },
//       },
//       {
//           path: '/info',
//           name: 'EmployeeInfoCenter',
//           component: EmployeeInfoCenter,
//           meta: {
//               title: '员工信息中心',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/talent',
//           name: 'TalentPool',
//           component: TalentPool,
//           meta: {
//               title: '人才库',
//               isTabsPage: true,
//           }
//       },
//       {
//           path: '/report-internship',
//           name: 'ReportInternship',
//           component: ReportInternship,
//           meta: {
//               title: '实习转正报表',
//               isTabsPage: true,
//           }
//       }, {
//           path: '/report-station',
//           name: 'ReportStationTrans',
//           component: ReportStationTrans,
//           meta: {
//               title: '岗位调动报表',
//               isTabsPage: true,
//           },
//       },
//       {
//           path: '/report-dept',
//           name: 'ResportDeptTrans',
//           component: ResportDeptTrans,
//           meta: {
//               title: '部门调动报表',
//               isTabsPage: true,
//           },
//       }, {
//           path: '/report-month',
//           name: 'ReportEntryAndDism',
//           component: ReportEntryAndDism,
//           meta: {
//               title: '人事月报',
//               isTabsPage: true,
//           }
//       }, {
//           path: '/internship',
//           name: 'Internship',
//           component: Internship,
//           meta: {
//               title: '试用期管理',
//               isTabsPage: true,
//           }
//       }, {
//           path: '/internship-log',
//           name: 'InternshipLog',
//           component: InternshipLog,
//           meta: {
//               title: '转正记录',
//               isTabsPage: true,
//           }
//       },
//   ],
// },

const router = new VueRouter({
    routes,
    mode: 'history',
})

router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.meta.title) {
        document.title = to.meta.title + ' | 1024'
    }
    if (to.path !== '/login') {
        // 如果还未登录，进行拦截
        if (!store.getters.isLogin) {
            Message.error('您还未登录,请先登录!')
            return next('/login')
        }
        // 权限控制
        if (to.meta.requireAuth && !store.getters.isAdmin) {
            Message.error('您无权限访问')
            return next('/401')
        }

        // 判断是否需要添加到tabs标签
        if (to.meta.isTabsPage) {
            const tabsItem = {
                name: to.name,
                title: to.meta.title,
                path: to.path,
            }
            store.commit('ADD_TABS_VALUE', tabsItem)
        }
    }
    next()
})

router.afterEach(() => {
    NProgress.done()
})
export default router